import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_API_BASE_PATH),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue')
    },
    {
      path: '/',
      name: 'home',
      redirect: '/panel',
      component: () => import('@/views/home/home.vue'),
      children: [
        {
          path: 'panel',
          name: 'panel',
          meta: { title: '仪表盘' },
          component: () => import('@/views/instrument/panel.vue')
        },
        {
          path: 'merchantReport',
          name: 'merchantReport',
          meta: { title: '商户报表' },
          component: () => import('@/views/instrument/merchantReport.vue')
        },
        {
          path: 'player',
          name: 'player',
          redirect: '/playerAccount',
          meta: { title: '玩家数据' },
          children: [
            {
              path: 'playerAccount',
              name: 'playerAccount',
              meta: { title: '玩家账号' },
              component: () => import('@/views/player/playerAccount.vue')
            },
            {
              path: 'playerLoginLog',
              name: 'playerLoginLog',
              meta: { title: '登录日志' },
              component: () => import('@/views/player/playerLoginLog.vue')
            },
            {
              path: 'gameRecord',
              name: 'gameRecord',
              meta: { title: '游戏记录' },
              props: true, // 启用 props 接收路由参数
              component: () => import('@/views/player/gameRecord.vue')
            },
            {
              path: 'walletTransfer',
              name: 'walletTransfer',
              meta: { title: '钱包转账记录' },
              component: () => import('@/views/player/walletTransfer.vue')
            },
            {
              path: 'onlineTrends',
              name: 'onlineTrends',
              meta: { title: '在线走势' },
              component: () => import('@/views/player/onlineTrends.vue')
            },
            {
              path: 'userStatistics',
              name: 'userStatistics',
              meta: { title: '用户统计' },
              component: () => import('@/views/player/userStatistics.vue')
            },
            {
              path: 'orderList',
              name: 'orderList',
              meta: { title: '订单列表' },
              component: () => import('@/views/player/orderList.vue')
            }
          ]
        },
        {
          path: 'gameReport',
          name: 'gameReport',
          redirect: '/merchantDaily',
          meta: { title: '游戏报表' },
          children: [
            {
              path: 'merchantDaily',
              name: 'merchantDaily',
              meta: { title: '商户每日数据' },
              component: () => import('@/views/gameReport/merchantDaily.vue')
            },
            {
              path: 'gameRemain',
              name: 'gameRemain',
              meta: { title: '投注留存' },
              component: () => import('@/views/gameReport/gameRemain.vue')
            },
            {
              path: 'firstRemain',
              name: 'firstRemain',
              meta: { title: '首次投注留存' },
              component: () => import('@/views/gameReport/firstRemain.vue')
            },
            {
              path: 'gameWinning',
              name: 'gameWinning',
              meta: { title: '游戏输赢' },
              component: () => import('@/views/gameReport/gameWinning.vue')
            },
            {
              path: 'gameRank',
              name: 'gameRank',
              meta: { title: '排行榜' },
              component: () => import('@/views/gameReport/gameRank.vue')
            }
          ]
        },
        {
          path: 'merchant',
          name: 'merchant',
          redirect: '/generalAgent',
          meta: { title: '商户管理' },
          children: [
            {
              path: 'generalAgent',
              name: 'generalAgent',
              meta: { title: '总代管理' },
              component: () => import('@/views/merchantManage/generalAgent/generalAgent.vue')
            },
            {
              path: 'merchant',
              name: 'merchant',
              meta: { title: '商户列表' },
              component: () =>
                import('@/views/merchantManage/merchant/merchant.vue')
            },
            {
              path: 'merchantLoginLog',
              name: 'merchantLoginLog',
              meta: { title: '商户登录日志' },
              component: () => import('@/views/merchantManage/merchantLoginLog.vue')
            },
            {
              path: 'merchantBehaviorLog',
              name: 'merchantBehaviorLog',
              meta: { title: '商户行为日志' },
              component: () => import('@/views/merchantManage/merchantBehaviorLog.vue')
            }
          ]
        },
        {
          path: 'configManage',
          name: 'configManage',
          redirect: '/winLose',
          meta: { title: '配置管理' },
          children: [
            {
              path: 'winLose',
              name: 'winLose',
              meta: { title: '游戏水位观察' },
              component: () => import('@/views/configManage/gameWater.vue')
            },
            {
              path: 'gameJili',
              name: 'gameJili',
              meta: { title: 'JILI数值效果' },
              component: () => import('@/views/configManage/gameJili.vue')
            },
            {
              path: 'maintenance',
              name: 'maintenance',
              meta: { title: '维护' },
              component: () => import('@/views/configManage/maintenance.vue')
            },
            {
              path: 'gameConfig',
              name: 'gameConfig',
              meta: { title: '游戏配置' },
              component: () => import('@/views/configManage/gameConfig.vue')
            },
            {
              path: 'globalConfig',
              name: 'globalConfig',
              meta: { title: '全局表配置' },
              component: () => import('@/views/configManage/globalConfig.vue')
            },
            {
              path: 'defaultConfig',
              name: 'defaultConfig',
              meta: { title: '游戏默认配置' },
              component: () => import('@/views/configManage/defaultConfig.vue')
            },
            {
              path: 'playerControl',
              name: 'playerControl',
              meta: { title: '游戏RTP调控' },
              component: () => import('@/views/configManage/playerControl.vue')
            },
            {
              path: 'menuConfig',
              name: 'menuConfig',
              meta: { title: '菜单管理' },
              component: () => import('@/views/configManage/menuConfig/menuConfig.vue')
            },
          ]
        },
        {
          path: '/accountManagement',
          name: 'accountManagement',
          redirect: '/account',
          meta: { title: '账号管理' },
          children: [
            {
              path: 'account',
              name: 'account',
              meta: { title: '后台账号' },
              component: () => import('@/views/accountManage/account/account.vue')
            },
            {
              path: 'accountPermissions',
              name: 'accountPermissions',
              meta: { title: '账号权限' },
              component: () => import('@/views/accountManage/permissions.vue')
            },
            {
              path: 'generalRole',
              name: 'generalRole',
              meta: { title: '后台角色管理' },
              component: () => import('@/views/accountManage/generalRole.vue')
            },
            {
              path: 'generalAccount',
              name: 'generalAccount',
              meta: { title: '后台账号管理' },
              component: () => import('@/views/accountManage/general/generalAccount.vue')
            },
            {
              path: 'loginLog',
              name: 'loginLog',
              meta: { title: '登录日志' },
              component: () => import('@/views/accountManage/loginLog.vue')
            },
            {
              path: 'behaviorLog',
              name: 'behaviorLog',
              meta: { title: '行为日志' },
              component: () => import('@/views/accountManage/behaviorLog.vue')
            }
          ]
        },
        {
          path: 'document',
          name: 'document',
          meta: { title: 'API接入' },
          component: () => import('@/views/instrument/document.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.name == 'login') {
    sessionStorage.removeItem('PGtoken')
    next()
  } else {
    localStorage.setItem('path', to.path as string)
    if (!sessionStorage.getItem('PGtoken')) {
      next({ name: 'login' })
    } else {
      next()
    }
  }
})

// 用于处理页面防止一段时间之后 路由点不动的情况
router.onError(error => {
  if (error.message.includes("Failed to fetch dynamically imported module")) {
    // reload()：类似于点击浏览器上的刷新页面按钮
    window.location.reload();
  }
});
export default router
